package com.bsb.biz.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.bsb.base.model.BsbExpertVoice;
import com.bsb.base.model.BsbExpertVoiceExample;
import com.bsb.biz.service.BsbExpertVoiceService;

@Controller
@RequestMapping("/bsbExpertVoice")
public class BsbExpertVoiceController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/bsbExpertVoice/list.do";
	
	@Resource
	private BsbExpertVoiceService bsbExpertVoiceService;
	
	@RequiresPermissions("bsbExpertVoice:view")
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String list(
			Model model,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		BsbExpertVoiceExample bsbExpertVoiceExample = new BsbExpertVoiceExample();
		int totalCount = bsbExpertVoiceService.countBsbExpertVoice(bsbExpertVoiceExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		bsbExpertVoiceExample.setPageIndex((pageNum - 1) * pageSize);
		bsbExpertVoiceExample.setPageSize(pageSize);
		List<BsbExpertVoice> bsbExpertVoices = bsbExpertVoiceService.getPageBsbExpertVoice(bsbExpertVoiceExample);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("bsbExpertVoices", bsbExpertVoices);
		return "/BsbExpertVoice/list";
	}
	
	@RequiresPermissions("bsbExpertVoice:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String toAdd(Model model) {
		return "/BsbExpertVoice/add";
	}

	@RequiresPermissions("bsbExpertVoice:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String add(Model model, @ModelAttribute("bsbExpertVoice") BsbExpertVoice bsbExpertVoice) {
		bsbExpertVoiceService.addBsbExpertVoice(bsbExpertVoice);
		return redirect;
	}
	
	@RequiresPermissions("bsbExpertVoice:delete")
	@RequestMapping(value = "/{uuid}/delete", method = RequestMethod.GET)
	public String del(Model model,
			@PathVariable("uuid") String uuid) {
		bsbExpertVoiceService.delBsbExpertVoiceByUuid(uuid);
		return redirect;
	}
	
	@RequiresPermissions("bsbExpertVoice:update")
	@RequestMapping(value = "/{uuid}/update", method = RequestMethod.GET)
	public String toEdit(Model model,
			@PathVariable("uuid") String uuid) {
		BsbExpertVoice bsbExpertVoice = bsbExpertVoiceService.getBsbExpertVoiceByUuid(uuid);
		model.addAttribute("bsbExpertVoice", bsbExpertVoice);
		return "/BsbExpertVoice/edit";
	}

	@RequiresPermissions("bsbExpertVoice:update")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String edit(Model model, @ModelAttribute("bsbExpertVoice") BsbExpertVoice bsbExpertVoice) {
		bsbExpertVoiceService.updateBsbExpertVoice(bsbExpertVoice);
		return redirect;
	}
	
	@RequiresPermissions("bsbExpertVoice:view")
	@RequestMapping(value = "/{uuid}/view", method = RequestMethod.GET)
	public String view(Model model,
			@PathVariable("uuid") String uuid) {
		BsbExpertVoice bsbExpertVoice = bsbExpertVoiceService.getBsbExpertVoiceByUuid(uuid);
		model.addAttribute("bsbExpertVoice", bsbExpertVoice);
		return "/BsbExpertVoice/view";
	}
}
